﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Mani.Common;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;
using System.Collections.Generic;

public partial class NewInternalAccounting : BasePage {

    private IInternalAccountingService internalAccountingService;
    private ISalesPersonService salesPersonService;

    public ISalesPersonService SalesPersonService
    {
        get { return salesPersonService; }
        set { salesPersonService = value; }
    }

    public IInternalAccountingService InternalAccountingService
    {
        get { return internalAccountingService; }
        set { internalAccountingService = value; }
    }

    public override string ActionName {
        get {
            return "vw_newinternalaccounting";
        }
    }

    protected void Page_Init(object sender, EventArgs e) {
        if ( Request.QueryString["count"] == null )
            generateControls(1);
        else
            generateControls(Convert.ToInt32(Request.QueryString["count"]));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (CommonHelper.ShowHiddenCustomers()) {
            divType.Visible = true;
        }
        else {
            divType.Visible = false;
        }

        GetInternalAccountingTypes();
    }

    private void GetInternalAccountingTypes()
    {
        ddlInternalAccountingType.DataSource = internalAccountingService.GetInternalAccountingTypes();
        ddlInternalAccountingType.DataBind();
    }

    protected void btnSave_Click( object sender, EventArgs e ) {
            InsertAccounting();
    }

    private void InsertAccounting() {
        int count = (Request.QueryString["count"] == null) ? 1 : Convert.ToInt32(Request.QueryString["count"]);
        for (int i = 0; i < count; i++) {

            bool paid = ((CheckBox)phInternalAccountingRows.FindControl("cbPaid" + i)).Checked;
            int type = Convert.ToInt32(ddlInternalAccountingType.SelectedValue);
            DropDownList dropdownSalesPerson = (DropDownList)phInternalAccountingRows.FindControl("ddlSalesPerson" + i);
            int salesPersonId = Convert.ToInt32( dropdownSalesPerson.SelectedValue );
            Single quantity = Convert.ToSingle(((TextBox)phInternalAccountingRows.FindControl("txtQuantity" + i)).Text);
            Single unitPrice = Convert.ToSingle(((TextBox)phInternalAccountingRows.FindControl("txtUnitPrice" + i)).Text);
            Single debit = quantity * unitPrice;
            DropDownList dropdownAccounts = (DropDownList)phInternalAccountingRows.FindControl("ddlAccounts" + i);
            DropDownList dropdownItems = (DropDownList)phInternalAccountingRows.FindControl("ddlItems" + i);

            InternalAccounting iaccounting = new InternalAccounting();
            iaccounting.Account = internalAccountingService.GetAccount(Convert.ToInt32(dropdownAccounts.SelectedValue));
            iaccounting.Credit = 0;
            iaccounting.Debit = debit;
            iaccounting.Item = internalAccountingService.GetItem(Convert.ToInt32(dropdownItems.SelectedValue));
            iaccounting.Quantity = quantity;
            iaccounting.UnitPrice = unitPrice;
            iaccounting.SalesPerson = salesPersonService.FindById(salesPersonId);
            iaccounting.AccountingType = internalAccountingService.GetInternalAccountingType(type);
            iaccounting.Paid = paid;
            iaccounting.Deleted = false;
            iaccounting.ModifyStamp = DateTime.Now;

            internalAccountingService.NewInternalAccounting(iaccounting);


            UIHelper.ClientAlert(this, "با موفقیت ثبت شد");
            UIHelper.ClientRedirect(this, "InternalAccounting.aspx");
        }
    }

    private void generateControls(int count) {

        IList<InternalAccount> accounts = internalAccountingService.GetAccounts();
        IList<SalesPerson> salesPersons = salesPersonService.FindAll();
        IList<Item> items = internalAccountingService.GetItems();

        for (int i = 0; i < count; i++) {
            HtmlTableRow row = new HtmlTableRow();
            
            HtmlTableCell td1 = new HtmlTableCell();
            DropDownList drpAccounts = new DropDownList();
            drpAccounts.ID = "ddlAccounts" + i;
            drpAccounts.DataSource = accounts;
            drpAccounts.DataTextField = "Name";
            drpAccounts.DataValueField = "Id";
            drpAccounts.DataBind();
            td1.Controls.Add(drpAccounts);

            HtmlTableCell td2 = new HtmlTableCell();
            DropDownList drpSalesPerson = new DropDownList();
            drpSalesPerson.ID = "ddlSalesPerson" + i;
            drpSalesPerson.DataSource = salesPersons;
            drpSalesPerson.DataTextField = "Title";
            drpSalesPerson.DataValueField = "Id";
            drpSalesPerson.DataBind();
            td2.Controls.Add(drpSalesPerson);


            HtmlTableCell td3 = new HtmlTableCell();
            DropDownList drpItems = new DropDownList();
            drpItems.ID = "ddlItems" + i;
            drpItems.DataTextField = "Name";
            drpItems.DataValueField = "Id";
            drpItems.DataSource = items;
            drpItems.DataBind();
            td3.Controls.Add( drpItems );

            HtmlTableCell td4 = new HtmlTableCell();
            HtmlTableCell td5 = new HtmlTableCell();
            HtmlTableCell td6 = new HtmlTableCell();

            TextBox txtQuantity = new TextBox();
            TextBox txtUnitPrice = new TextBox();
            TextBox txtAmount = new TextBox();

            txtQuantity.ID = "txtQuantity" + i;
            txtQuantity.Width = 50;
            txtQuantity.Attributes.Add("autocomplete", "off");

            txtUnitPrice.ID = "txtUnitPrice" + i;
            txtUnitPrice.Width = 100;
            txtUnitPrice.Attributes.Add("autocomplete", "off");

            txtAmount.ID = "txtAmount" + i;
            txtAmount.Width = 100;
            txtAmount.Enabled = false;

            td4.Controls.Add(txtQuantity);
            td5.Controls.Add(txtUnitPrice);
            td6.Controls.Add(txtAmount);

            HtmlTableCell td7 = new HtmlTableCell();
            CheckBox cbPaid = new CheckBox();
            cbPaid.ID = "cbPaid" + i;
            td7.Controls.Add(cbPaid);


            row.Controls.Add(td1);
            row.Controls.Add(td2);
            row.Controls.Add(td3);
            row.Controls.Add(td4);
            row.Controls.Add(td5);
            row.Controls.Add(td6);
            row.Controls.Add(td7);
            phInternalAccountingRows.Controls.Add(row);


            string javascript = "javascript:calculatePrice(@txtQuantity,@txtUnitPrice,@txtAmount)";
            javascript = javascript.Replace("@txtQuantity", txtQuantity.ClientID);
            javascript = javascript.Replace("@txtUnitPrice", txtUnitPrice.ClientID);
            javascript = javascript.Replace("@txtAmount", txtAmount.ClientID);

            txtQuantity.Attributes.Add("onchange", javascript);
            txtUnitPrice.Attributes.Add("onchange", javascript);
        }
    }

    protected void btnSaveItem_Click(object sender, EventArgs e) {
        Item item = new Item();
        item.Name = txtItemName.Text;
        item.Unit = txtItemUnit.Text;
        item.Deleted = false;
        item.ModifyStamp = DateTime.Now;
        item.CreateDate = DateTime.Now;
        internalAccountingService.PersistItem(item);


        if (Request.QueryString["count"] == null)
            AddItem(1, item.Id.ToString(), item.Name);
        else
            AddItem(Convert.ToInt32(Request.QueryString["count"]), item.Id.ToString(), item.Name);

        txtItemName.Text = "";
        txtItemUnit.Text = "";
        lblMessage.Text = "کالا با موفقیت ذخیره شد";
    }

    private void AddItem(int count, string value, string text)
    {
        for (int i = 0; i < count; i++)
        {
            Control control = phInternalAccountingRows.FindControl("ddlItems" + i);
            if (control != null)
            {
                DropDownList drpItem = (DropDownList)control;
                drpItem.Items.Add(new ListItem(text, value));
            }
        }
    }
    
    protected void btnGenerateRows_Click(object sender, EventArgs e) {
        Response.Redirect("NewInternalAccounting.aspx?count=" + txtCount.Text);
    }
}
